// pages/addressList/addressList.js
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    isLoading: 1,//是否显示加载中图标
    addressList: [],
    page: 1
  },
  // 设置订单收货地址
  setAddress: function (e) {
    var _this = this;
    if (_this.data.orderId) {
      var requestData = {
        token: _this.data.token,
        order_id: _this.data.orderId,
        address_id: e.currentTarget.dataset.address_id
      }
      app.post('/Order/setAddress', requestData, function (res) {
        if (res.code == 2000) {
          wx.navigateBack({
            delta: 1
          })
        }else{
          wx.showToast({
            title: res.msg,
            icon: "none",
            duration: 2000
          });
        }
      });
    }
  },
  // 设置默认收获地址
  setDefault: function (e) {
    var _this = this;
    var requestData = {
      token: _this.data.token,
      address_id: e.currentTarget.dataset.address_id
    }
    app.post('/User/defaultAddress', requestData, function (res) {
      if (res.code == 2000) {
        wx.showToast({
          title: '设置默认收货地址成功',
          icon: 'none',
          duration: 2000,
          success: function () {
            _this.setData({
              addressList: [],
              page: 1
            })
            _this.getAddressList();
          }
        })
      }else{
        wx.showToast({
          title: res.msg,
          icon: "none",
          duration: 2000
        });
      }
    });
  },
  // 删除收货地址
  deletAddresss: function (e) {
    var _this = this;
    var formId = e.detail.formId;
    app.postFormId(_this.data.token, formId);
    wx.showModal({
      title: '删除提示',
      content: '确定要删除当前地址吗',
      success(res) {
        if (res.confirm) {
          var requestData = {
            token: _this.data.token,
            address_id: e.currentTarget.dataset.address_id
          }
          app.post('/User/delAddress', requestData, function (res) {
            if (res.code == 2000) {
              wx.showToast({
                title: '删除成功',
                icon: 'success',
                duration: 2000,
                success: function () {
                  _this.setData({
                    addressList: [],
                    page: 1
                  })
                  _this.getAddressList();
                }
              })
            }else{
              wx.showToast({
                title: res.msg,
                icon: "none",
                duration: 2000
              });
            }
          });
        } else if (res.cancel) {
        }
      }
    })
   
  },
  // 编辑收货地址
  changeAddress: function (e) {
    var _this = this;
    var formId = e.detail.formId;
    app.postFormId(_this.data.token, formId);
    var address_id = e.currentTarget.dataset.address_id;
    wx.navigateTo({
      url: '/pages/addAddress/addAddress?address_id=' + address_id,
    })
  },
  // 获取收货地址列表
  getAddressList: function () {
    var _this = this;
    var requestData = {
      token: _this.data.token,
      page: _this.data.page
    }
    app.post('/User/getAddressList', requestData, function (res) {
      if (res.code == 2000) {
        wx.hideNavigationBarLoading(); //完成停止加载
        wx.stopPullDownRefresh(); //停止下拉刷新
        var addressList = _this.data.addressList;
        addressList = addressList.concat(res.data.list);
        _this.setData({
          isLoading:0,
          page: res.data.page,
          addressList: addressList
        })
      }else{
        wx.showToast({
          title: res.msg,
          icon: "none",
          duration: 2000
        });
      }
    });
  },
  // 手动添加收货地址
  goAddAddress: function (e) {
    var _this = this;
    var formId = e.detail.formId;
    app.postFormId(_this.data.token, formId);
    wx.navigateTo({
      url: '/pages/addAddress/addAddress',
    })
  },
  // 通过微信添加收货地址
  addFormWechat: function (e) {
    var _this = this;
    var formId = e.detail.formId;
    app.postFormId(_this.data.token, formId);
    wx.chooseAddress({
      success: function (res) {
        var requestData = {
          token: _this.data.token,
          user_name: res.userName,
          tel_number: res.telNumber,
          province_name: res.provinceName,
          city_name: res.cityName,
          county_name: res.countyName,
          detail_info: res.detailInfo,
          add_type: 1
        }
        app.post('/User/addAddress', requestData, function (res) {
          if (res.code == 2000) {
            wx.showToast({
              title: '添加地址成功',
              icon: 'success',
              duration: 2000
            })
          }else{
            wx.showToast({
              title: res.msg,
              icon: "none",
              duration: 2000
            });
          }
        });
      },
      fail:function(res){
        wx.showModal({
          title: '提示',
          content: '需要授权通讯地址哦~',
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var _this = this;
    if (options.orderId) {
      _this.setData({
        orderId: options.orderId
      })
    }
  },
  // 判断是否获取到token的值了
  loop: function () {
    var _this = this;
    var theToken = wx.getStorageSync("token");
    if (!theToken) {
      setTimeout(function () { _this.loop() }, 100);
    } else {
      _this.setData({
        token: theToken
      });
      _this.getAddressList();
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var _this = this;
    _this.setData({
      addressList: [],
      page: 1
    })
    _this.loop();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var _this = this;
    wx.showNavigationBarLoading(); //在标题栏中显示加载
    _this.setData({
      addressList: [],
      page: 1
    })
    _this.getAddressList();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var _this = this;
    _this.getAddressList();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})